package com.zhengdongzulin.zuke.ext

import android.text.Editable
import android.text.InputFilter
import android.text.SpannableString
import android.text.style.LeadingMarginSpan
import android.widget.ImageView
import android.widget.TextView
import com.lxj.androidktx.core.color
import com.lxj.easyadapter.ViewHolder
import net.lucode.hackware.magicindicator.buildins.UIUtil

private val REGEX_MOBILE = "^[1][3,4,5,6,7,8][0-9]{9}\$"
private val REGEX_ID = "^[1-9]\\d{5}(18|19|20)\\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]\$"

/**
 * 获取text
 */
fun TextView.getTextZ(): String {
    return this.getText().toString().trim()
}

/**
 * textview的内容是否为null
 */
fun TextView.isEmpty(): Boolean {
    return this.getText().toString().trim().isEmpty()
}

/**
 * 检查手机号
 */
fun TextView.checkPhone(): Boolean {
    if (Regex(REGEX_MOBILE).matches(this.getTextZ())) {
        return true
    }
    return false
}

/**
 * 检查身份证
 */
fun TextView.checkId(): Boolean {
    if (Regex(REGEX_ID).matches(this.getTextZ())) {
        return true
    }
    return false
}

/**
 * 设置最大输入长度
 */
fun TextView.setMaxLength(length: Int) {
    filters = arrayOf<InputFilter>(InputFilter.LengthFilter(length))
}

/**
 * 检查输入长度
 */
fun TextView.contrast(contrast: Int = 1): Boolean = this.getTextZ().length >= contrast
fun Editable.contrast(contrast: Int = 1): Boolean = this.length >= contrast

/**
 * 设置textview标签图片的边距
 */
fun TextView.setDrawableInTxt(img: ImageView?, dp: Double, str: String) {
    val spannablestring = SpannableString(str)
    val what =
            LeadingMarginSpan.Standard(img?.layoutParams?.width!! + UIUtil.dip2px(context, dp), 0)
    spannablestring.setSpan(what, 0, spannablestring.length, 0)
    this.text = spannablestring
}

fun TextView.setDrawLeft(id: Int) {
    var drawable = context.getDrawable(id)
//    drawable?.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
//    setCompoundDrawables(drawable, null, null, null)
    setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null)
}

fun TextView.setDrawRight(id: Int) {
    var drawable = context.getDrawable(id)
    setCompoundDrawablesWithIntrinsicBounds(null, null, drawable, null)
}

fun TextView.setColor(color: Int) {
    setTextColor(color(color))
}

fun TextView.setTint(id: Int) {
    for (compoundDrawable in this.compoundDrawables) {
        compoundDrawable?.setTint(color(id))
    }
}

fun ViewHolder.setColor(viewId: Int,color: Int ) {
    val view = getView<TextView>(viewId)
    view.setColor(color)
}

fun ViewHolder.setOnClickListenerr(viewId: Int,onClick:(TextView)->Unit) {
    val view = getView<TextView>(viewId)
    view.setOnClickListener {
        onClick(it as TextView)
    }
}